package com.atguigu.springboot.service.impl;

import com.atguigu.springboot.entities.Attraction;
import com.atguigu.springboot.entities.AttractionExample;
import com.atguigu.springboot.entities.Register;
import com.atguigu.springboot.entities.RegisterExample;
import com.atguigu.springboot.mapper.AttractionMapper;
import com.atguigu.springboot.mapper.RegisterMapper;
import com.atguigu.springboot.service.AttractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("attractionService")
public class AttractionServiceImpl implements AttractionService {

    @Autowired
    AttractionMapper attractionMapper;
    @Autowired
    RegisterMapper registerMapper;

    @Override
    public List<Attraction> getAllAttraction() {
        AttractionExample attractionExample=new AttractionExample();
        AttractionExample.Criteria criteria=attractionExample.createCriteria();
        criteria.andAidIsNotNull();
      return   attractionMapper.selectByExample(attractionExample);
    }

    @Override
    public List<Attraction> getRegisterAttraction(int uid) {

        RegisterExample registerExample=new RegisterExample();
        RegisterExample.Criteria criteria=registerExample.createCriteria();
        criteria.andUidEqualTo(uid);
       List<Register> list= registerMapper.selectByExample(registerExample);
     if(list.size()==0) return new ArrayList<>();
        AttractionExample attractionExample=new AttractionExample();
        AttractionExample.Criteria criteria1=attractionExample.createCriteria();
       criteria1.andAidIn(list.stream().map(a->a.getAid()).collect(Collectors.toList()));
      return attractionMapper.selectByExample(attractionExample);
    }

    @Override
    public List<Attraction> getNoRegisterAttraction(int uid) {
        List<Attraction> all=getAllAttraction();
        List<Attraction> visited= getRegisterAttraction(uid);

    return     all.stream().filter(a->!visited.contains(a)).collect(Collectors.toList());
    }

    @Override
    public Attraction getAttraction(int aid) {
       return  attractionMapper.selectByPrimaryKey(aid);
    }
}
